/*   Copyright © 2009 Mark Gil Torres
 *
 *   This file is part of mtoolkit.
 *
 *   mtoolkit is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   mtoolkit is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with mtoolkit.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../include/rational.h"
#define BOOST_TEST_MODULE Rational test
#include <boost/test/included/unit_test.hpp>

using namespace mt;

BOOST_AUTO_TEST_CASE(constructor_test) {
  Rational x1;
  BOOST_CHECK_EQUAL(x1.getNumerator(),0);
  BOOST_CHECK_EQUAL(x1.getDenominator(),1);

  Rational x2(5);
  BOOST_CHECK_EQUAL(x2.getNumerator(),5);
  BOOST_CHECK_EQUAL(x2.getDenominator(),1);

  Rational x3(5,3);
  BOOST_CHECK_EQUAL(x3.getNumerator(),5);
  BOOST_CHECK_EQUAL(x3.getDenominator(),3);
http://start.ubuntu.com/9.04/
  Rational x4(x3);
  BOOST_CHECK_EQUAL(x4.getNumerator(),5);
  BOOST_CHECK_EQUAL(x4.getDenominator(),3);

  Rational x5("1234567890");
  Rational x6("1","2");
  BOOST_CHECK_EQUAL(x5.getNumerator(),1234567890);
  BOOST_CHECK_EQUAL(x5.getDenominator(),1);
  BOOST_CHECK_EQUAL(x6.getNumerator(),1);
  BOOST_CHECK_EQUAL(x6.getDenominator(),2);

  Rational x7(6,2);
  BOOST_CHECK_EQUAL(x7.getNumerator(),3);
  BOOST_CHECK_EQUAL(x7.getDenominator(),1);

  Rational x8(0,10);
  BOOST_CHECK_EQUAL(x8.getNumerator(),0);
  BOOST_CHECK_EQUAL(x8.getDenominator(),1);

  Rational x9("6","2");
  BOOST_CHECK_EQUAL(x9.getNumerator(),3);
  BOOST_CHECK_EQUAL(x9.getDenominator(),1);

  Rational x10("0","10");
  BOOST_CHECK_EQUAL(x10.getNumerator(),0);
  BOOST_CHECK_EQUAL(x10.getDenominator(),1);

  Integer x11("1234567890");
  Integer x12("1234567891");
  Rational x13(x11,x12);
  BOOST_CHECK_EQUAL(x13.getNumerator(),1234567890);
  BOOST_CHECK_EQUAL(x13.getDenominator(),1234567891);

  Rational x14(1,2);
  Rational x15(3,2);
  Rational x16, x17, x18;
  x16 = x14 + 1;
  x18 = 0 * x17;
  BOOST_CHECK_EQUAL(x16.getNumerator(),3);
  BOOST_CHECK_EQUAL(x16.getDenominator(),2);
  BOOST_CHECK_EQUAL(x18.getNumerator(),0);
  BOOST_CHECK_EQUAL(x18.getDenominator(),1);
}

BOOST_AUTO_TEST_CASE(assignment_test) {
  Rational x1;
  Rational x2;
  Rational x3(2,3);
  x2 = x3;
  BOOST_CHECK_EQUAL(x2.getNumerator(),2);
  BOOST_CHECK_EQUAL(x2.getDenominator(),3);
  x2 = x1;
  BOOST_CHECK_EQUAL(x2.getNumerator(),0);
  BOOST_CHECK_EQUAL(x2.getDenominator(),1);

  x1.setNumerator(10);
  x1.setDenominator(15);
  BOOST_CHECK_EQUAL(x1.getNumerator(),2);
  BOOST_CHECK_EQUAL(x1.getDenominator(),3);
  x2.setNumerator("20");
  x2.setDenominator("25");
  BOOST_CHECK_EQUAL(x2.getNumerator(),4);
  BOOST_CHECK_EQUAL(x2.getDenominator(),5);
  Integer x4 = 5;
  x3.setDenominator(x4);
  BOOST_CHECK_EQUAL(x3.getNumerator(),2);
  BOOST_CHECK_EQUAL(x3.getDenominator(),5);
}

BOOST_AUTO_TEST_CASE(arithmetic_test) {
  Rational x1(1,2);
  Rational x2(1,4);
  Rational x3(3,4);
  Rational x4(1,8);
  Rational x5;
  x5 = x1 + x2;
  BOOST_CHECK_EQUAL(x5.getNumerator(),x3.getNumerator());
  BOOST_CHECK_EQUAL(x5.getDenominator(),x3.getDenominator());
  Rational x6;
  x6 = x1 - x2;
  BOOST_CHECK_EQUAL(x6.getNumerator(),x2.getNumerator());
  BOOST_CHECK_EQUAL(x6.getDenominator(),x2.getDenominator());
  Rational x7;
  x7 = x1 * x2;
  BOOST_CHECK_EQUAL(x7.getNumerator(),x4.getNumerator());
  BOOST_CHECK_EQUAL(x7.getDenominator(),x4.getDenominator());
  Rational x8;
  x8 = x1 / x2;
  BOOST_CHECK_EQUAL(x8.getNumerator(),2);
  BOOST_CHECK_EQUAL(x8.getDenominator(),1);
  Rational x9;
  Integer x10 = 1;
  x9 = x1 + x10;
  BOOST_CHECK_EQUAL(x9.getNumerator(),3);
  BOOST_CHECK_EQUAL(x9.getDenominator(),2);
  Rational x11;
  x11 = x10 + x1;
  BOOST_CHECK_EQUAL(x11.getNumerator(),3);
  BOOST_CHECK_EQUAL(x11.getDenominator(),2);
}

BOOST_AUTO_TEST_CASE(relation_test) {
  Rational x1(1, 2);
  Rational x2(2, 4);
  Rational x3(3, 4);
  Rational x4;
  BOOST_CHECK_EQUAL((x1==x2), true);
  BOOST_CHECK_EQUAL((x1<=x2), true);
  BOOST_CHECK_EQUAL((x1>=x2), true);
  BOOST_CHECK_EQUAL((x1<x3), true);
  BOOST_CHECK_EQUAL((x3>x4), true);
  BOOST_CHECK_EQUAL((x2>=x3), false);

  Integer x5 = 2;
  Integer x6 = 0;
  BOOST_CHECK_EQUAL((x1==x5), false);
  BOOST_CHECK_EQUAL((x4==x6), true);
  BOOST_CHECK_EQUAL((x6<x3), true);
  BOOST_CHECK_EQUAL((x6<=x1), true);
  BOOST_CHECK_EQUAL((x5>x3), true);
  BOOST_CHECK_EQUAL((x6>=x1), false);

  BOOST_CHECK_EQUAL((x1<2), true);
  BOOST_CHECK_EQUAL((x1>=0), true);
  BOOST_CHECK_EQUAL((0>x3), false);

}
